import 'package:efood_multivendor/data/model/response/product_model.dart';

class CartModel {
  double? _price;
  double? _discountedPrice;
  List<Variation>? _variation;
  double? _discountAmount;
  int? _quantity;
  List<AddOn>? _addOnIds;
  List<AddOns>? _addOns;
  double? _totalAddonPrice;

  bool? _isCampaign;
  Product? _product;

  CartModel(
      double? price,
      double? discountedPrice,
      List<Variation>? variation,
      double? discountAmount,
      int? quantity,
      List<AddOn>? addOnIds,
      List<AddOns>? addOns,
      bool? isCampaign,
      Product? product,
      double? totalAddonPrice) {
    this._price = price;
    this._discountedPrice = discountedPrice;
    this._variation = variation;
    this._discountAmount = discountAmount;
    this._quantity = quantity;
    this._addOnIds = addOnIds;
    this._addOns = addOns;
    this._isCampaign = isCampaign;
    this._product = product;
    this._totalAddonPrice = totalAddonPrice;
  }

  double? get price => _price;
  double? get totalAddonPrice => _totalAddonPrice;
  // set pricewithaddon(double price) {
  //   _price = price;
  // }

  // set priceWithoutAddon(double price) {
  //   _price = price;
  // }

  double? get discountedPrice => _discountedPrice;
  List<Variation>? get variation => _variation;
  double? get discountAmount => _discountAmount;
  // ignore: unnecessary_getters_setters
  int get quantity => _quantity ?? 0;
  // ignore: unnecessary_getters_setters
  set quantity(int qty) => _quantity = qty;
  List<AddOn>? get addOnIds => _addOnIds;
  List<AddOns>? get addOns => _addOns;
  bool? get isCampaign => _isCampaign;
  Product? get product => _product;
  set discountAmount(double? amount) {
    _discountAmount = amount;
  }

  set totalAddonPrice(double? total) {
    _totalAddonPrice = total;
  }

  set discountedPrice(double? amount) => _discountedPrice = amount;

  set addAddonIds(AddOn addOn) {
    _addOnIds?.add(addOn);
  }

  CartModel.fromJson(Map<String, dynamic> json) {
    _price = json['price'].toDouble();
    _discountedPrice = json['discounted_price'].toDouble();
    if (json['variation'] != null) {
      _variation = [];
      json['variation'].forEach((v) {
        _variation?.add(new Variation.fromJson(v));
      });
    }
    _discountAmount = json['discount_amount'].toDouble();
    _quantity = json['quantity'];
    if (json['add_on_ids'] != null) {
      _addOnIds = [];
      json['add_on_ids'].forEach((v) {
        _addOnIds?.add(new AddOn.fromJson(v));
      });
    }
    if (json['add_ons'] != null) {
      _addOns = [];
      json['add_ons'].forEach((v) {
        _addOns?.add(new AddOns.fromJson(v));
      });
    }
    _isCampaign = json['is_campaign'];
    if (json['product'] != null) {
      _product = Product.fromJson(json['product']);
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['price'] = this._price;
    data['discounted_price'] = this._discountedPrice;
    if (this._variation != null) {
      data['variation'] = this._variation?.map((v) => v.toJson()).toList();
    }
    data['discount_amount'] = this._discountAmount;
    data['quantity'] = this._quantity;
    if (this._addOnIds != null) {
      data['add_on_ids'] = this._addOnIds?.map((v) => v.toJson()).toList();
    }
    if (this._addOns != null) {
      data['add_ons'] = this._addOns?.map((v) => v.toJson()).toList();
    }
    data['is_campaign'] = this._isCampaign;
    data['product'] = this._product?.toJson();
    return data;
  }
}

class AddOn {
  int? _id;
  int? _quantity;
  double? _price;
  double? _totalPrice;

  AddOn({int? id, int? quantity, double? price, double? totalPrice}) {
    this._id = id;
    this._quantity = quantity;
    this._price = price;
    this._totalPrice = totalPrice;
  }

  int? get id => _id;
  int? get quantity => _quantity;
  double? get price => _price;
  double? get totalPrice => _totalPrice;

  set incrementAddonQuantity(int quantity) {
    _quantity = (_quantity ??= 0) + 1;
  }

  set decrementAddonQuantity(int quantity) {
    _quantity = (_quantity ??= 0) - 1;
  }

  set setPrice(double price) {
    this._price = price;
  }

  AddOn.fromJson(Map<String, dynamic> json) {
    _id = json['id'];
    _quantity = json['quantity'];
    _price = json['price'];
    _totalPrice = json['totalPrice'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this._id;
    data['quantity'] = this._quantity;
    data['price'] = this._price;
    data['totalPrice'] = this._totalPrice;
    return data;
  }
}
